package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingLevelDTO;
import com.zzyl.entity.NursingLevel;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.NursingLevelVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    /**
     * 查询所有护理等级
     *
     * @return
     */
    @Override
    public List<NursingLevelVO> findAll() {

        List<NursingLevelVO> vo = nursingLevelMapper.findAll();
        return vo;
    }

    @Override
    public void insertNursingLevel(NursingLevelDTO nursingLevelDTO) {
        //首先判断护理等级的名称是否重复，若重复返回自定义异常
       NursingLevel nl= nursingLevelMapper.selectByName(nursingLevelDTO.getName());
       if (ObjectUtil.isNotEmpty(nl)){
           throw new BaseException(BasicEnum.NURSE_LEVEL_NAME_REPEAT_INSERT_FAIL);
       }
       //如果护理等级名称不重复，新增护理等级
        nursingLevelDTO.setStatus(0);
       nursingLevelMapper.insertNursingLevel(nursingLevelDTO);
    }

    @Override
    public PageResponse<NursingLevelVO> listByPage(Integer pageNum, Integer pageSize, Integer status, String name) {
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);

        //通过数据库进行分页查询
       Page<List<NursingLevel>> list= nursingLevelMapper.listByPage(pageNum,pageSize,status,name);
       return PageResponse.of(list, NursingLevelVO.class);
    }

    @Override
    public NursingLevelVO selectById(Long id) {
        NursingLevelVO vo=nursingLevelMapper.selectById(id);
        return  vo;
    }

    @Override
    public void updateNursingLevel(NursingLevelDTO nursingLevelDTO) {
        //首先判断护理等级的名称是否重复，若重复返回自定义异常，再判断护理等级的名称是否是
        NursingLevel nl= nursingLevelMapper.selectByName(nursingLevelDTO.getName());
//        if (ObjectUtil.isNotEmpty(nl) && nl.getName()!=nursingLevelDTO.getName()){
//            throw new BaseException(BasicEnum.NURSE_LEVEL_NAME_REPEAT_INSERT_FAIL);
//        }
        nursingLevelMapper.updateNursingLevel(nursingLevelDTO);

    }

    @Override
    public void editStatus(Integer id, Integer status) {
        nursingLevelMapper.editStatus(id,status);
    }

    @Override
    public void deleteById(Integer id) {
        nursingLevelMapper.deleteById(id);
    }


}
